package cn.cdeden.resource.service.impl;

import cn.cdeden.common.core.utils.ServletUtils;
import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.core.utils.ip.RegionUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.resource.api.domain.RemoteFilesDownload;
import cn.cdeden.resource.domain.bo.SysOssDownloadRecordBo;
import cn.cdeden.resource.domain.vo.SysOssDownloadRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cdeden.resource.domain.SysOssDownloadRecord;
import cn.cdeden.resource.mapper.SysOssDownloadRecordMapper;
import cn.cdeden.resource.service.ISysOssDownloadRecordService;

import java.util.Collection;
import java.util.List;

/**
 * 文件下载记录Service业务层处理
 *
 * @author Yu.
 * @date 2024-12-08
 */
@RequiredArgsConstructor
@Service
public class SysOssDownloadRecordServiceImpl implements ISysOssDownloadRecordService {

    private final SysOssDownloadRecordMapper baseMapper;


    @Override
    public boolean save(RemoteFilesDownload storage, HttpServletRequest request) {
        return baseMapper.insert(generate(storage, request))  > 0;
    }

    @Override
    public SysOssDownloadRecord generate(RemoteFilesDownload storage, HttpServletRequest request) {
        SysOssDownloadRecord record = new SysOssDownloadRecord();
        record.setFileId(storage.getDateId());//临时存储为 文件编号
        record.setFileType(storage.isFolder()? "folder" :"file");//文件类型
        record.setName(storage.getFileNameSuffix());//软件名称
        record.setOssId(String.valueOf(storage.getStorageId()));//软件实体编号
        record.setVersionId(storage.getVersionId());//文件版本号
        record.setVersionName(storage.getVersionName());//文件版
        record.setModuleType(storage.getLibraryType());//库的类型
//        record.setIdentity(storage.getSourceFile());//下载者身份
        String ip = ServletUtils.getClientIP();;//获取ip地址
//        String browser = StringUtils.getBrowser(request); //浏览器
//        String equipment = StringUtils.getEnd(request).getValue();//设备
        String address = RegionUtils.getCityInfo(ip); //地址
        record.setLoginIp(ip);//获取ip地址
//        record.setBrowser(browser);//浏览器
//        record.setEquipment(equipment);//设备
        record.setAddress(address);//地址
        return record;
    }


    /**
     * @author Yu.
     * @Description // 批量插入
     * @Date 2024/12/9 9:53
     * @param records
     * @return boolean
     **/
    public boolean saveList(List<SysOssDownloadRecord> records) {
        return baseMapper.insertBatch(records);
    }

    @Override
    public List<SysOssDownloadRecordVo> selectRecordList(SysOssDownloadRecordBo bo) {
        LambdaQueryWrapper<SysOssDownloadRecord> lqw = buildQueryWrapper(bo);
        List<SysOssDownloadRecordVo> list = baseMapper.selectVoList(lqw);
        return list;
    }

    @Override
    public TableDataInfo<SysOssDownloadRecordVo> selectRecordPage(SysOssDownloadRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOssDownloadRecord> lqw = buildQueryWrapper(bo);
        IPage<SysOssDownloadRecordVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public Boolean deleteDownloadByModuleId(Collection<String> moduleId, OssModuleType type, boolean isDeleteFile, Collection<String> userIds) {
        LambdaQueryWrapper<SysOssDownloadRecord> wrapper = Wrappers.lambdaQuery(SysOssDownloadRecord.class)
            .in(!isDeleteFile, SysOssDownloadRecord::getFileId, moduleId)
            .in(isDeleteFile, SysOssDownloadRecord::getOssId, moduleId)
            .eq(SysOssDownloadRecord::getModuleType, type.getCode())
            .in(userIds!=null,SysOssDownloadRecord::getCreateBy,userIds)
            ;
        return baseMapper.delete(wrapper) > 0;
    }

    /**
     * 构建查询条件
     * @param bo
     * @return
     */
    private LambdaQueryWrapper<SysOssDownloadRecord> buildQueryWrapper(SysOssDownloadRecordBo bo) {
        LambdaQueryWrapper<SysOssDownloadRecord> lqw = Wrappers.lambdaQuery(SysOssDownloadRecord.class);
        lqw.eq(StringUtils.isNotBlank(bo.getModuleType()), SysOssDownloadRecord::getModuleType, bo.getModuleType());
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), SysOssDownloadRecord::getOssId, bo.getOssId());
        lqw.orderByDesc(SysOssDownloadRecord::getCreateTime); // 创建时间降序
        return lqw;
    }




}
